//
//  LoginViewController.m
//  KiklinkApp
//
//  Created by LF on 14/10/21.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "LoginViewController.h"
#import "API.h"
#import "LoginNavigationViewController.h"


@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (strong, nonatomic) IBOutlet UIView *backgroundView;

@end

//各种原因退出时均应该考虑此回调
Callback loginCallback = ^(int stateCode, NSString* word, id content)
{
};

@implementation LoginViewController

- (void)viewDidLoad {
	[super viewDidLoad];
//    self.accountTextField.text = @"18811721373";
//    self.passwordTextField.text = @"123456789";
    
	// Do any additional setup after loading the view.
	self.accountTextField.delegate = self;
	self.passwordTextField.delegate = self;
	
	LoginNavigationViewController *navi = (LoginNavigationViewController*) self.navigationController;
	loginCallback = [navi loginCallback];
	
	UITapGestureRecognizer *backgroundTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard)];
	[self.backgroundView addGestureRecognizer:backgroundTapRecognizer];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

-(void)closeKeyboard
{
	[self.passwordTextField resignFirstResponder];
	[self.accountTextField resignFirstResponder];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (BOOL)textFieldShouldReturn:(UITextField *) textField
{
	if(textField == self.accountTextField)
	{
		[self.passwordTextField becomeFirstResponder];
	}
	else if(textField == self.passwordTextField)
	{
		[self login];
	}
	return YES;
}

- (IBAction)onClickQuitButton:(id)sender {
	[self dismissViewControllerAnimated:YES completion:nil];
	loginCallback(FAILURE, @"用户退出登录界面", nil);
}

- (IBAction)onClickLoginButton:(id)sender {
	[self login];
}

-(void)login
{
	if([self.accountTextField.text length] == 0)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入账号、密码" message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
		[alert show];
		
		return;
	}
	
	if(! [AccountManager isPasswordValid: self.passwordTextField.text])
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"密码过短" message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
		[alert show];
		return;
	}
	
	[AccountManager firstLoginKiklinkWithUsername:self.accountTextField.text password:self.passwordTextField.text callback:^(int stateCode, NSString *word, id content) {
		KLLog(self, content);
		
		if(stateCode == FAILURE_RETURN_ERRER_CODE)
		{
			showAlert(word);
			return;
		}
		else if(stateCode < 0)
		{
			showAlert(@"登录失败");
			return;
		}
		else
		{
			[self dismissViewControllerAnimated:YES completion:nil];
			loginCallback(stateCode, word, content);
		}
	}];
}

//SNS登录成功则自动退出本界面，否则保持不动，不管错误提示
- (IBAction)onClickWeiboLogin:(id)sender {
	KLLogWithFormat(self, @"isLoggedinTo Weibo: %@", [SNSManager isLoggedInWithPlatform:UMShareToSina]?@"YES":@"NO");
	
	[AccountManager loginSNSWithPlatform:UMShareToSina callback:^(int stateCode, NSString *word, id content) {
		
		if(stateCode == SUCCESS)
		{
			[AccountManager openBindPhoneUIWithUserData:content callback:^(int stateCode, NSString *word, id content) {
				if(stateCode == SUCCESS)
				{
					[self dismissViewControllerAnimated:YES completion:nil];
					loginCallback(stateCode, word, content);
				}
			}];
		}
	}];
}

- (IBAction)onClickQQLogin:(id)sender {
	KLLogWithFormat(self, @"isLoggedinTo QQ: %@", [SNSManager isLoggedInWithPlatform:UMShareToQQ]?@"YES":@"NO");
	//	if(! [SNSManager isLoggedInWithPlatform:UMShareToQQ])
	//	{
	[SNSManager openSNSLoginUIWithPlatform:UMShareToQQ ViewController:self callback:^(int stateCode, NSString *word, id content) {
		if(stateCode == SUCCESS)
		{
			[self dismissViewControllerAnimated:YES completion:nil];
		}
		loginCallback(stateCode, word, content);
	}];
	//	}
}

@end
